home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / bipl.zip / DOCS.ZIP / IPP.DOC < prev    next >
Text File  |  1992-03-27  |  7KB  |  262 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.             An Icon Pre-Processor
  8.  
  9.  
  10.                 Frank J. Lhota
  11.              Mei Associates, Inc.
  12.              1050 Waltham Street
  13.                Lexington, MA 02173-8024
  14.             Voice: (617) 862-3390
  15.              FAX: (617) 862-5053
  16.  
  17.  
  18.  
  19.     The Icon Programming Library comes with an Icon preprocessor
  20. called IPP.  I have made several enhancements to this program, and I
  21. would like to submit the enhanced version of the IPP to the IPL.
  22.  
  23.  
  24.  
  25.  
  26.  
  27.               New IPP features
  28.  
  29.     For those who are not familiar with the IPP, the header comments
  30. in the IPP.ICN file provide complete intructions on its use.  The rest
  31. of this section assumes a familiarity with the previous version of
  32. the IPP.
  33.  
  34.     This new version of the IPP processes #line directives, which can
  35. be used to change the value of the _LINE_ and _FILE_ symbols.  Also,
  36. the new IPP wiil generates #line directives when needed, so that the
  37. preprocessor output will always indicate the original source of its
  38. text.  As a result, if we pipe the output of IPP to icont, e.g.,
  39.  
  40.     iconx ipp.icx foo.icn | icont -ofoo -
  41.  
  42. then (assuming that the source itself does not have any line
  43. directives) the &file and &line keywords refer to the lines in the
  44. original source file, not to "stdin" and the line numbers of the IPP
  45. output.  The #line directives will be generated even when other
  46. comments are being stripped from the input.  
  47.  
  48.     The preprocessor command syntax has been relaxed a bit.  The
  49. basic form of a preprocessor command line is still
  50.  
  51.     $command [arguments]
  52.  
  53. but now the user is permitted to include spaces around the '$', so
  54. that preproccessor commands can have a pretty-print look, e.g.
  55.  
  56.     $ifndef FOO
  57.       $if BAR = 0
  58.         $ define FOO -1
  59.       $else
  60.         $ define FOO BAR
  61.       $endif
  62.     $endif    # ndef FOO
  63.  
  64.  
  65.  
  66.                 - 1 -
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.     On non-UNIX systems, the new IPP has a more liberal search
  76. algorithm for $include files.  For files enclosed with <>, the
  77. directories specified in the IPATH environment variable are searched
  78. for the file.  The search for file enclosed in "" starts with the
  79. current directory, then proceeds to the IPATH directories.  As
  80. before, the -I command line option can be used to add directories to
  81. the beginning of the standard search path.
  82.  
  83.     The following preprocessor commands have been added to IPP:
  84.  
  85.     $elif:
  86.         Invoked as 'elif constant-expression'.  If the lines
  87.     preceding this command were processed, this command and the
  88.     lines following it up to the matching $endif command are
  89.     ignored.  Otherwise, the constant-expression is evaluated,
  90.     and the lines following this command are processed only if
  91.     it produces a result.
  92.   
  93.     $error:
  94.         This command issues a error, with the text coming from
  95.     the argument field of the command.  As with all errors,
  96.     processing is terminated.
  97.  
  98.     $warning:
  99.         This command issues a warning, with the text coming from
  100.     the argument field of the command.
  101.  
  102.     In addition to the operators previously supported, the constant
  103. expressions appearing in $if / $elif command can now use the unary
  104. versions of the '+' and '-' operators, and the 'not' control
  105. structure.  Also, backtracking is used in the evaluation of constant
  106. expressions, so that when the command 
  107.  
  108.     $if FOO = (2|3)
  109.  
  110. is processed, the lines following it are processed precisely when
  111. either FOO equals 2 or FOO equals 3.
  112.  
  113.  
  114.                Uses of the IPP
  115.  
  116.     To understand the following examples, the reader should keep in
  117. mind this feature of the IPP:  The IPP creates a pre-defined symbol
  118. out of each string generated by &features.  These symbols are created
  119. by taking the non-letter characters of the &features strings and
  120. replacing them with underscores.  Thus, if &features includes UNIX,
  121. the symbol UNIX is defined; if co-expressions are supported, the
  122. symbol co_expressions is defined, and so on.
  123.  
  124.     The IPP can be an handy tool for distributing Icon programs that
  125. require some customization for specific implementations.  A prime
  126. example of this is the IPP itself.  IPP must be able to contruct a
  127.  
  128.  
  129.  
  130.  
  131.                 - 2 -
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139. full pathname for a file, given a directory and file name.  On many
  140. systems, this is done by performing the catenation
  141.  
  142.     directory || "/" || filename
  143.  
  144. This file naming convention is not, however, universal.  On DOS and 
  145. OS/2 systems, "\\" should be used instead of "/" to separate the
  146. directory and filename.  Under VMS, the separator should be "".
  147.  
  148.     To accomodate these system-dependant variations, the IPP source
  149. (in the file IPP.ICN, on this disk) is written using the symbol
  150. DIR_SEP for the string that separates the directory and filename
  151. portions of a complete path.  The IPP code starts with the
  152. preprocessor directives:
  153.  
  154.     $ifndef DIR_SEP
  155.       $ifdef UNIX
  156.         $define DIR_SEP "/"
  157.       $elif def(MS_DOS) | def(MS_DOS_386) | def(OS_2)
  158.         $define DIR_SEP "\\"
  159.       $elif def(VMS)
  160.         $define DIR_SEP ""
  161.       $else
  162.         $error Need a definition for DIR_SEP
  163.       $endif
  164.     $endif # ndef DIR_SEP
  165.  
  166. After preprocessing this code, DIR_SEP will be "/" on UNIX systems,
  167. and "\\" on DOS and OS/2 systems.  For other systems, an appropriate
  168. value for DIR_SEP could be specified on the preprocessor command line
  169. by using the -D options, e.g.
  170.  
  171.     ipp -D DIR_SEP=\"\" ipp.ipp ipp.icn
  172.  
  173.     Another example of Icon software that could exploit IPP
  174. customization is BINCVT, the IPL package of utilities for converting
  175. between integers and their internal binary representations.  The
  176. version of BINCVT currently included in the IPL assumes a
  177. "big-endian" system. On "big-endian" systems, the bytes in the binary
  178. representation of an integer are arranged from most significant to
  179. least significant. However, major platforms such as the IBM PC family
  180. or the VAX machines use the "little-endian" method for storing
  181. integers, in which the bytes representing an integer go from least
  182. significant to most significant.  
  183.  
  184.     Using IPP, one can write a version of BINCVT that can be
  185. preprocessed to produce a working package for either big-endian or
  186. little-endian systems.  The symbol LITTLE_ENDIAN will be defined (via
  187. the command line option -D LITTLE_ENDIAN) to produce output for
  188. little endian systems.  Most of the functions in BINCVT can be
  189. expressed in terms of starting at the most significant byte, and
  190. moving to the less significant bytes.  Hence, the generalized BINCVT
  191. starts with the definitions:
  192.  
  193.  
  194.  
  195.  
  196.                 - 3 -
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.     $ifdef LITTLE_ENDIAN
  206.        $define GOTO_BIG_END tab (0)
  207.        $define TO_SMALL_END -1
  208.     $else
  209.        $define GOTO_BIG_END
  210.        $define TO_SMALL_END 1
  211.     $endif
  212.  
  213.     Using these definitions, we can write a version of the unsigned
  214. function that will work for either integer storage method:
  215.  
  216.     procedure unsigned(s)
  217.       local result
  218.       result := 0
  219.       s ? {
  220.         GOTO_BIG_END
  221.         while result := ord(move(TO_SMALL_END)) + result * 16r100
  222.         }
  223.       return result
  224.     end
  225.  
  226. The file BINCVT.IPP on this disk contains the source code for this
  227. example.
  228.  
  229.  
  230.                  Conclusions
  231.  
  232.     The IPP allows Icon programmers to write more flexable and more
  233. portable code.  The latest version of the IPP is easier to use and
  234. more powerful than the previous version.
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.                 - 4 -
  262.